package merch;

use strict;
use warnings;
use Plugins;
use Log qw(message);

use LWP::UserAgent;
use Network;
use Network::Receive;
use CGI;
use Encode qw/ is_utf8 /;

my $ua = LWP::UserAgent->new();
#my $url = "http://merchantslist.servegame.com/items";
my $site = "http://merchantslist.cu.cc/";
my $url = $site . "items";
my $soldUrl = $site . "update";
my $token = "o0UO4J9zNlKodoo54hmNYjJki5GbiiIEbhaxtPts";
my $agent = "Mozilla/5.0 (Windows NT 6.2; WOW64)";
my $api = "Y2FkZWF0aA==";
my $chatTitle = "LOOKING for ITEM? PM me";
my $wpm = 60; #(char/wpm)*10 = delay

my @greets = ("hello po", "hehe hai", "hey!", "good day po");
my @share = ("ito po ang website: ". $site, "prototype pa po ito, " . $site, "Check mo po: ". $site);

my $charname = shift;
my $item = shift;
my $cost = shift;
my $location = shift;

my $openRoom = 0;

Plugins::register("Merch", "Vending Client for Ragnarok", \&on_unload, \&on_reload);
my $console = Commands::register(["webpost", "Sample post to web", \&post_Item]);
my $hooks = Plugins::addHooks(
		['AI_pre',\&main, undef],
		['packet/private_message', \&onPM, undef]
	);

sub on_unload {
	# This plugin is about to be unloaded; remove hooks	
	Plugins::delHooks($hooks);
	Commands::unregister($console);
}

sub on_reload {	
	&on_unload;
}

sub main{
	#Create ROOM
	if ($openRoom == 0){
		Commands::run("chat create \"" . $chatTitle . "\" 2 0 es ");
		$openRoom = 1;
	}
}

sub onPM {
	my ($packet, $args) = @_;

	my $msg = $args->{privMsg};
	my $pmUser = $args->{privMsgUser};

	#Sold
	#format:
	#sold <code>
	if($msg =~ /^sold/) {
		my $parse = substr($msg,5);

		my $chk = sold_Item($parse);
		if($chk = 1){
			botReply($pmUser,"Item " . $msg . " is now sold! Thank you");
			return 1;
		}else{
			botReply($pmUser,"Item not found...");
			return 1;
		}
	}

	#Posting
	#format:
	#merch <item name/description>,<[~]price|nego>,[fld x y]
	if($msg =~ /^merch/){
			my $parse = substr($msg,6);
			botReply($pmUser,"Got it! and...");
			botReply($pmUser,$parse,1);

			#Commands::run('webpost ' $parse);
			$parse = $pmUser.",".$parse;
			my $code = post_Item('webpost',$parse);
			if($code eq 0){
					botReply($pmUser, "Service Failed to post...");
				}else{
					botReply($pmUser, "if ITEM is sold, PM in this format: sold " . $code,1);
				}			
		}else
		{			
			#botReply($pmUser,"Invalid Format");
			onMarket($pmUser);
		}
}

sub onMarket {
	my ($user) = @_;
	my $grt = int(rand(4));
	my $shr = int(rand(3));

	my $reply = shift;
	$reply = $greets[$grt];
	botReply($user,$reply);
	$reply = $share[$shr];
	botReply($user,$reply);
}

sub sold_Item {
	my ($code) = @_;
	$ua->agent($agent);
	my $response = $ua->post( $soldUrl,[
			'_token'=> $token,
			'api'	=> $api,
			'code'	=> $code
		]);
	if($response->is_success){
		return 1;
	}else{
		return 0;
	}
}

sub post_Item {
	my ($command, $sitepost) = @_;
	#parsing arguments
	message "Arguments: " . $sitepost . "\n";
	my @tmps = split /,/, $sitepost;
	$charname = $tmps[0];
	$item = $tmps[1];
	$cost = $tmps[2];
	$location = $tmps[3];

	message "Posting...\n";	
	$ua->agent($agent);
	my $response = $ua->post( $url,[
			'_token'	=> $token,
			'api'	=> $api,
			'ch'	=> $charname,
			'itm'	=> $item,
			'c'		=> $cost,
			'loc'	=> $location
		]);

	if($response->is_success){
		my $page = $response->decoded_content();
		message "Code: " . $page . "\n";

		return $page;
	}else{
		message "Error" . $response->status_line . "\n";
		return 0;
	}
}

# botReply <user> <wpm> <msg>
sub botReply {
	my($pmUser,$msg,$flag) = @_;
	my $delay = (length($msg)/$wpm) * 10;
	if ($flag eq undef) {sleep($delay);}
	Commands::run('pm "' . $pmUser . '" ' . $msg);
}

1;